<?php
//测试控制器类
class CityController extends CommonController{
    //动作方法
    private $model;
    public function __init(){
    	parent::__init();
    	$this->model = K('City');
    }
    // 主页
    public function index(){
    	// 获得城市数据
        $cityData=$this->model->get_data();
        $cityData=Data::tree($cityData,'cityname','ctid','ct_pid');
        $this->assign('cityData',$cityData);
        $this->display();
    }
    // 增加城市
    public function add(){
    	if(IS_POST){
    		$cityname=Q('post.cityname',0,'trim');
    		$first=Q('post.first',0,'strtoupper');
    		$ctid=Q('post.ctid',0,'intval');
    		$data=array(
                'cityname'=>$cityname,
                'first'=>$first,
                'ct_pid'=>$ctid,
    		);
            $data=$this->model->add_data($data);
            if($data){
            	$this->success('添加成功',U('City/index'));
            }
            $this->error($this->model->error);
    	}
        // 获得城市分类
        $cityData=$this->model->get_data();
        $cityData=Data::tree($cityData,'cityname','ctid','ct_pid');
        $this->assign('cityData',$cityData);
        $this->display();
    }
    // 修改城市
    public function edit(){
    	if(IS_POST){    
    		$ctid=Q('post.ctid',0,'intval');		
    		$cityname=Q('post.cityname',0,'trim');
    		$first=Q('post.first',0,'strtoupper');
    		$pid=Q('post.ct_pid',0,'intval');    		
    		$data=array(
               'cityname'=>$cityname,
               'first'=>$first,
               'ct_pid'=>$pid,
    		);
    		$data=$this->model->update_data($ctid,$data);
    		if($data){
    			$this->success('修改成功',U('City/index'));
    		}
    		$this->error($this->model->error);
    	}
    }
    // 删除城市
    public function del(){
    	$ctid=Q('get.ctid',0,'intval');
    	$data=$this->model->dele($ctid);
    	if($data){
    		$this->success('删除成功',U('City/index'));
    	}
    	$this->error($this->model->error);
    }
}